home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / interapplication comm / 7edit / source / sveditwindow.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  28.3 KB  |  1,123 lines

  1. /*
  2.     File:        SVEditWindow.c
  3.  
  4.     Contains:    
  5.  
  6.     Written by: Original version by Jon Lansdell and Nigel Humphreys.
  7.                 3.1 updates by Greg Sutton.    
  8.  
  9.     Copyright:    Copyright © 1995-1999 by Apple Computer, Inc., All Rights Reserved.
  10.  
  11.                 You may incorporate this Apple sample source code into your program(s) without
  12.                 restriction. This Apple sample source code has been provided "AS IS" and the
  13.                 responsibility for its operation is yours. You are not permitted to redistribute
  14.                 this Apple sample source code as "Apple sample source code" after having made
  15.                 changes. If you're going to re-distribute the source, we require that you make
  16.                 it clear in the source that the code was descended from Apple sample source
  17.                 code, but that you've made changes.
  18.  
  19.     Change History (most recent first):
  20.                 7/19/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1 and removed GX printing(now obsolete
  21.                 11/1/95      DS                 Made Changes for GX Printing.
  22.                 10/12/95     CW                 Cleaned up DoContent and added call to DoWindowContentDrag
  23.                                              Clean up CloseMyWindow and added call to RemoveDragHandlers
  24.                                              Added InstallDragHandlers call to NewDocument
  25.                                              Use HiliteControl on suspend/resume instead of Show/HideControl
  26.                 
  27.  
  28. */
  29.  
  30. #include <Scrap.h>
  31. #include <Packages.h>
  32. #include <DriverServices.h>
  33. #include <TextUtils.h>
  34.  
  35. #include "SVEditWindow.h"
  36. #include "SVDrag.h"
  37. #include "SVAERecording.h"
  38. #include "SVEditPrinting.h"
  39.  
  40. #define    kControlInvisible        0
  41. #define    kControlVisible            0xFF             
  42. #define    kScrollbarWidth            16
  43. #define    kScrollbarAdjust        (kScrollbarWidth - 1)
  44. #define    kScrollTweek            2
  45. #define    kTextOffset                5 
  46. #define    kButtonScroll            10
  47.             
  48. #define    kHOffset                20   // Stagger window offsets
  49. #define    kVOffset                20
  50.             
  51. #define    kTBarHeight                20
  52. #define    kMBarHeight                20
  53.  
  54. #pragma segment Window
  55.  
  56. DPtr    DPtrFromWindowPtr(WindowPtr theWindow)
  57. {
  58.     if (theWindow)
  59.         return((DPtr)GetWRefCon(theWindow));
  60.     else
  61.         return(NULL);
  62. } // DPtrFromWindowPtr
  63.  
  64.     
  65. #pragma segment main
  66.  
  67. /* 
  68.   Scroll the TERec around to match up to the potentially updated scrollbar
  69.   values. This is really useful when the window resizes such that the
  70.   scrollbars become inactive and the TERec had been previously scrolled.
  71. */
  72. pascal void AdjustTE(DPtr theDoc)
  73.   {
  74.        short    h;
  75.          short    v;
  76.          TEHandle myText;
  77.          
  78.          myText = theDoc->theText;
  79.        h = ((*myText)->viewRect.left - (*myText)->destRect.left) -
  80.           GetControlValue(theDoc->hScrollBar) + kTextOffset;
  81.                          
  82.          v = ((*myText)->viewRect.top - (*myText)->destRect.top) - 
  83.               GetControlValue(theDoc->vScrollBar) + kTextOffset;
  84.                                  
  85.      if (h || v)
  86.              {
  87.                TEScroll(h, v, theDoc->theText);
  88.                DrawPageExtras(theDoc);
  89.              }
  90.                     
  91.     }  /* AdjustTE */
  92.     
  93.     
  94. /*Calculate the new control maximum value and current value, whether it is the horizontal or
  95. vertical scrollbar. The vertical max is calculated by comparing the number of lines to the
  96. vertical size of the viewRect. The horizontal max is calculated by comparing the maximum document
  97. width to the width of the viewRect. The current values are set by comparing the offset between
  98. the view and destination rects. If necessary and we canRedraw, have the control be re-drawn by
  99. calling ShowControl.*/
  100.  
  101. /*TEStyleSample-vertical max originally used line by line calculations-lineheight was a
  102. constant value so it was easy to figure out what the range should be and pin the value
  103. within range. Now we need to use max and min values in pixels rather than in nlines*/
  104.  
  105. #pragma segment main
  106.  
  107. pascal void AdjustHV(Boolean isVert, ControlHandle control, DPtr theDoc, Boolean canRedraw)
  108.   {
  109.       TEHandle    docTE;
  110.     short       value;
  111.         short           max;
  112.         short           oldValue;
  113.         short           oldMax;
  114.         Rect             sizeRect;
  115.  
  116.         GetRectOfPage( theDoc, &sizeRect );
  117.         docTE    = theDoc->theText;
  118.     
  119.         oldValue = GetControlValue(control);
  120.     oldMax   = GetControlMaximum(control);
  121.     if (isVert)
  122.       {
  123.             /* new for TEStyleSample */
  124.                  max = TEGetHeight((*docTE)->nLines, 0, docTE) -
  125.                        ((*docTE)->viewRect.bottom - (*docTE)->viewRect.top);
  126.  
  127.        }
  128.     else
  129.       max = sizeRect.right - ((*docTE)->viewRect.right - (*docTE)->viewRect.left);
  130.                 
  131.         max += kTextOffset + kTextOffset; /* Allow over scroll by kTextOffset */
  132.                 
  133.     if (max < 0)
  134.       max = 0; /* check for negative values */
  135.             
  136.     SetControlMaximum(control, max);
  137.         
  138.       if (isVert)
  139.             value = (*docTE)->viewRect.top - (*docTE)->destRect.top;
  140.         else
  141.             value = (*docTE)->viewRect.left - (*docTE)->destRect.left;
  142.                     
  143.         value += kTextOffset;
  144.                 
  145.         if (value < 0)
  146.             value = 0;
  147.         else
  148.             if (value > max)
  149.                 value = max; /* pin the value to within range */
  150.                         
  151.         SetControlValue(control, value);
  152.         if (canRedraw && ((max != oldMax) || (value != oldValue)))
  153.             ShowControl(control); /* check to see if the control can be re-drawn */
  154.             
  155.     } /* AdjustHV */
  156.  
  157. #pragma segment Main
  158.  
  159. pascal void AdjustScrollValues(DPtr theDoc, Boolean canRedraw)
  160.  
  161. /* Simply call the common adjust routine for the vertical and horizontal scrollbars. */
  162.  
  163.   {        
  164.         AdjustHV(true,  theDoc->vScrollBar, theDoc, canRedraw);
  165.         AdjustHV(false, theDoc->hScrollBar, theDoc, canRedraw);
  166.     }        /* AdjustScrollValues */
  167.  
  168.  
  169. pascal void GetTERect(WindowPtr window, Rect *teRect)
  170.  
  171. /*   return a rectangle that is inset from the portRect by the size of
  172.      the scrollbars and a little extra margin. */
  173.  
  174.   {
  175.     *teRect = window->portRect;
  176.     (*teRect).bottom -= kScrollbarAdjust; /* and for the scrollbars */
  177.         (*teRect).right  -= kScrollbarAdjust;
  178.   }         /* GetTERect */
  179.  
  180. pascal void AdjustScrollSizes(DPtr theDoc)
  181.  
  182. /* Re-calculate the position and size of the viewRect and the scrollbars.
  183.   kScrollTweek compensates for off-by-one requirements of the scrollbars
  184.   to have borders coincide with the growbox. */
  185.  
  186.  {  
  187.    Rect    teRect;
  188.      Rect    myPortRect;
  189.  
  190.    GetTERect(theDoc->theWindow, &teRect); /*start with teRect*/
  191.      myPortRect = theDoc->theWindow->portRect;
  192.      
  193.      (*(theDoc->theText))->viewRect = teRect;
  194.  
  195.    MoveControl(theDoc->vScrollBar, myPortRect.right - kScrollbarAdjust, - 1);
  196.    SizeControl(theDoc->vScrollBar, 
  197.                  kScrollbarWidth, 
  198.                  (myPortRect.bottom - myPortRect.top) - (kScrollbarAdjust - kScrollTweek));
  199.             
  200.     MoveControl(theDoc->hScrollBar, - 1, myPortRect.bottom - kScrollbarAdjust);
  201.     SizeControl(theDoc->hScrollBar,
  202.                 (myPortRect.right - myPortRect.left) - (kScrollbarAdjust - kScrollTweek),
  203.                             kScrollbarWidth);
  204. }        /* AdjustScrollSizes */
  205.  
  206. #pragma segment Window
  207.  
  208. pascal void AdjustScrollbars(DPtr theDoc, Boolean  needsResize)
  209.  
  210. /* Turn off the controls by jamming a zero into their contrlVis fields
  211.   (HideControl erases them and we don't want that). If the controls are to
  212.   be resized as well, call the procedure to do that, then call the procedure
  213.   to adjust the maximum and current values. Finally reset the controls
  214.   to be visible if not in background. */
  215.   {
  216.  
  217.         (*(theDoc->vScrollBar))->contrlVis = kControlInvisible; /* turn them off */
  218.         (*(theDoc->hScrollBar))->contrlVis = kControlInvisible;
  219.  
  220.         if (needsResize) /* move and size if needed */
  221.             AdjustScrollSizes(theDoc);
  222.  
  223.         AdjustScrollValues(theDoc, !needsResize); /* fool with max and current value */
  224.  
  225.     /* Now, restore visibility in case we never had to ShowControl during adjustment */
  226.  
  227.         if (!gInBackground)
  228.             {
  229.                 (*(theDoc->vScrollBar))->contrlVis = kControlVisible; /* turn them on */
  230.                 (*(theDoc->hScrollBar))->contrlVis = kControlVisible;
  231.             }
  232.         else
  233.             { /* make sure they stay invisible */
  234.                 if ((*(theDoc->vScrollBar))->contrlVis)
  235.                     HideControl(theDoc->vScrollBar);
  236.                 if ((*(theDoc->vScrollBar))->contrlVis)
  237.                     HideControl(theDoc->hScrollBar);
  238.             }
  239.     }        /* AdjustScrollbars */
  240.  
  241. #pragma segment Window
  242.  
  243. pascal void GetWinContentRect(WindowPtr theWindow, Rect *r)
  244.     {
  245.         *r         = theWindow->portRect;
  246.         r->right  -= kScrollbarAdjust;
  247.         r->bottom -= kScrollbarAdjust;
  248.     }  /* GetWinContentRect */
  249.             
  250. #pragma segment Window
  251.         
  252. pascal void InvalidateDocument(DPtr theDoc)
  253.     {
  254.         GrafPtr oldPort;
  255.             
  256.     GetPort(&oldPort);
  257.         SetPort(theDoc->theWindow);
  258.         InvalRect(&theDoc->theWindow->portRect);
  259.         SetPort(oldPort);
  260.     } 
  261.                         
  262. pascal void DoResizeWindow(DPtr theDoc)
  263.  
  264. /* Called when the window has been resized to fix up the controls and content */
  265.  
  266.     {
  267.         AdjustScrollbars(theDoc, true);
  268.         AdjustTE(theDoc);
  269.         InvalidateDocument(theDoc);
  270.     }         /* ResizeWindow */
  271.  
  272. pascal void ResizePageSetupForDocument(DPtr theDoc)
  273.  
  274.     /* Called when the window has been resized to fix up the controls and content */
  275.  
  276.   {
  277.         Rect pageRect;
  278.         
  279.         GetRectOfPage( theDoc, &pageRect );
  280.         (*(theDoc->theText))->destRect.right = (*(theDoc->theText))->destRect.left
  281.                                                             + pageRect.right;
  282.         TECalText(theDoc->theText);
  283.         
  284.         DoResizeWindow(theDoc);
  285.     }         /* ResizePageSetupForDocument */
  286.  
  287. #pragma segment Main
  288.  
  289. pascal void CommonAction(ControlHandle control, short *amount)
  290.  
  291. /* Common algorithm for setting the new value of a control. It returns the actual amount
  292. the value of the control changed. Note the pinning is done for the sake of returning
  293. the amount the control value changed. */
  294.  
  295.     {
  296.         short   value;
  297.         short   max;
  298.  
  299.         value   = GetControlValue(control); /* get current value */
  300.         max     = GetControlMaximum(control); /* and max value */
  301.         *amount = value - *amount;
  302.         if (*amount < 0)
  303.           *amount = 0;
  304.         else
  305.             if (*amount > max)
  306.                 *amount = max;
  307.         
  308.         SetControlValue(control, *amount);
  309.         *amount = value - *amount; /* calculate true change */
  310.     }         /* CommonAction */
  311.  
  312. #pragma segment Main
  313.  
  314. pascal void VActionProc(ControlHandle control, short part)
  315.  
  316. /* Determines how much to change the value of the vertical scrollbar by and how
  317.   much to scroll the TE record. */
  318.  
  319.   {
  320.         short           amount;
  321.         WindowPtr       window;
  322.         DPtr            theDoc;
  323.         
  324.     if (part)
  325.       {
  326.         window = (*control)->contrlOwner;
  327.                 theDoc = DPtrFromWindowPtr(window);
  328.                 switch (part) {
  329.                   case kControlUpButtonPart:
  330.                     case kControlDownButtonPart : amount = 24;
  331.                                         break;
  332.                   case kControlPageUpPart:
  333.                     case kControlPageDownPart   : amount = (*(theDoc->theText))->viewRect.bottom - 
  334.                                                  (*(theDoc->theText))->viewRect.top;
  335.                                         break;
  336.                                                             
  337.                 }   /* case */
  338.                 
  339.                 if (part == kControlDownButtonPart || part == kControlPageDownPart)
  340.           amount = -amount; /* reverse direction */
  341.             
  342.               CommonAction(control, &amount);
  343.             
  344.                 if (amount)
  345.                     {
  346.                         TEScroll(0, amount, theDoc->theText);
  347.                         DrawPageExtras(theDoc);
  348.                     }
  349.             }     /* if */
  350.     }  /* VActionProc */
  351.  
  352. #pragma segment Main
  353.  
  354. pascal void HActionProc(ControlHandle control, short part)
  355.  
  356. /* Determines how much to change the value of the horizontal scrollbar by and how
  357.   much to scroll the TE record. */
  358.  
  359.  {
  360.    short      amount;
  361.    WindowPtr  window;
  362.    DPtr       theDoc;
  363.  
  364.         if  (part)
  365.             {
  366.                 window = (*control)->contrlOwner;
  367.                 theDoc = DPtrFromWindowPtr(window);
  368.                 switch (part) {
  369.                   case  kControlUpButtonPart   :
  370.                     case  kControlDownButtonPart : amount = kButtonScroll; /* a few pixels */
  371.                                          break;
  372.                   case  kControlPageUpPart   :
  373.                     case  kControlPageDownPart :   amount = (*(theDoc->theText))->viewRect.right - 
  374.                                                   (*(theDoc->theText))->viewRect.left; /* a page */
  375.                                          break;
  376.                 }   /* switch */
  377.                 if (part == kControlDownButtonPart || part == kControlPageDownPart)
  378.                     amount = - amount; /* reverse direction */
  379.                     
  380.                 CommonAction(control, &amount);
  381.                 if (amount)
  382.                     {
  383.                         TEScroll(amount, 0, theDoc->theText);
  384.                         DrawPageExtras(theDoc);
  385.                     }
  386.             }     /* if */
  387.     }         /* HActionProc */
  388.  
  389. /**-----------------------------------------------------------------------
  390.         Name:             ShowSelect
  391.         Purpose:        Scrolls the text selection into view.
  392.     -----------------------------------------------------------------------**/
  393.  
  394. #pragma segment Window
  395.  
  396. pascal void ShowSelect(DPtr theDoc)
  397.   {
  398.     AdjustScrollbars(theDoc, false);
  399.  
  400.         /*
  401.             Let TextEdit do the hard work of keeping the selection visible…
  402.         */
  403.  
  404.         TEAutoView(true, theDoc->theText);
  405.         TESelView(theDoc->theText);
  406.         TEAutoView(false, theDoc->theText);
  407.  
  408.         /*
  409.             Now rematch the text and the scrollbars…
  410.         */
  411.  
  412.         SetControlValue(theDoc->hScrollBar,
  413.                                 (*(theDoc->theText))->viewRect.left - 
  414.                                 (*(theDoc->theText))->destRect.left + kTextOffset);
  415.  
  416.         SetControlValue(theDoc->vScrollBar,
  417.                                 (*(theDoc->theText))->viewRect.top - 
  418.                                 (*(theDoc->theText))->destRect.top  + kTextOffset);
  419.  
  420.   }  /* ShowSelect */
  421.  
  422. #pragma segment Window
  423.  
  424. pascal void OffsetWindow(WindowPtr aWindow)
  425.   {
  426.      short theWidth;
  427.      short theHeight;
  428.      short theHScreen;
  429.      short theVScreen;
  430.      short xWidth;
  431.      short xHeight;
  432.      short hMax;
  433.      short vMax;
  434.      short wLeft;
  435.      short wTop;
  436.          
  437.      theWidth  = aWindow->portRect.right - aWindow->portRect.left;
  438.      theHeight = aWindow->portRect.bottom - aWindow->portRect.top + kTBarHeight;
  439.  
  440.      theHScreen = qd.screenBits.bounds.right  - qd.screenBits.bounds.left;
  441.      theVScreen = qd.screenBits.bounds.bottom - qd.screenBits.bounds.top;
  442.  
  443.      xWidth  = theHScreen - theWidth;
  444.      xHeight = theVScreen - (theHeight + kMBarHeight);
  445.  
  446.      hMax = (xWidth / kVOffset) + 1;
  447.      vMax = (xHeight / kVOffset) + 1;
  448.  
  449.          gWCount++;
  450.  
  451.      wLeft = (gWCount % hMax) * kVOffset;
  452.      wTop  = ((gWCount % vMax) * kVOffset) + kTBarHeight + kMBarHeight;
  453.  
  454.      MoveWindow(aWindow, wLeft, wTop, false);
  455.     }
  456.  
  457. pascal void GetLocalUpdateRgn(WindowPtr window, RgnHandle localRgn)
  458.  
  459.     /* Returns the update region in local coordinates */
  460.  
  461.     {
  462.         CopyRgn(((WindowPeek)window)->updateRgn, localRgn); /* save old update region */
  463.         OffsetRgn(localRgn, 
  464.                             window->portBits.bounds.left, 
  465.                             window->portBits.bounds.top); /* convert to local coords */
  466.     }          /* GetLocalUpdateRgn */
  467.  
  468. #pragma segment Window
  469.  
  470.  
  471.  
  472. pascal void MyGrowWindow(WindowPtr w, Point p)
  473.     {
  474.         GrafPtr savePort;
  475.     long    theResult;
  476.     Rect    r;
  477.  
  478.     GetPort(&savePort);
  479.     SetPort(w);
  480.     SetRect(&r, 80, 80, qd.screenBits.bounds.right, qd.screenBits.bounds.bottom);
  481.     theResult = GrowWindow(w, p, &r);
  482.         if (theResult)
  483.             IssueSizeWindow(w, LoWord(theResult), HiWord(theResult));
  484.  
  485.     SetPort(savePort);
  486.     }
  487.  
  488. #pragma segment Window
  489.  
  490. pascal void DoZoom(WindowPtr w,
  491.                    short     c,
  492.                    Point     p)
  493.  
  494.  {
  495.    GrafPtr savePort;
  496.  
  497.      GetPort(&savePort);
  498.    SetPort(w);
  499.      if (TrackBox(w, p, c))
  500.          {
  501.              EraseRect(&w->portRect);
  502.              IssueZoomCommand(w, c);
  503.          }
  504.     }
  505.  
  506. #pragma segment Window
  507.  
  508. pascal void DoContent ( WindowPtr theWindow, EventRecord theEvent )
  509. {
  510.     short            cntlCode;
  511.     short            part;
  512.     short            value;
  513.     ControlHandle    theControl;
  514.     GrafPtr            savePort;
  515.     Boolean            extend;
  516.     DPtr            theDoc;
  517.     Point            localPt;
  518.     
  519.     
  520.     GetPort ( &savePort );
  521.     SetPort ( theWindow );
  522.     theDoc = DPtrFromWindowPtr ( theWindow );
  523.     
  524.     localPt = theEvent.where;
  525.     GlobalToLocal ( &localPt );
  526.     cntlCode = FindControl ( localPt, theWindow, &theControl );
  527.     if ( cntlCode == 0 )
  528.     {
  529.         if ( UserWantsToDrag ( theWindow, theEvent.where ) )
  530.         {
  531.             if ( DoWindowContentDrag ( theWindow, &theEvent ) )
  532.                 TEClick ( localPt, false, theDoc->theText );
  533.         }
  534.         else
  535.         {
  536.             /*only extend the selection if the shiftkey is down*/
  537.             extend = (theEvent.modifiers & shiftKey);
  538.             if ( PtInDocument ( localPt, theDoc ) )
  539.                 TEClick ( localPt, extend, theDoc->theText );
  540.         }
  541.     }
  542.     else if ( cntlCode == kControlIndicatorPart )
  543.     {
  544.         value = GetControlValue(theControl);
  545.         part  = TrackControl(theControl, localPt, nil);
  546.         if (part)
  547.         {
  548.             value -= GetControlValue(theControl);
  549.             if (value)
  550.             {
  551.                 if (theControl == theDoc->vScrollBar)
  552.                     TEScroll(0, value, theDoc->theText);
  553.                 else
  554.                     TEScroll(value, 0, theDoc->theText);
  555.                 DrawPageExtras(theDoc);
  556.             }
  557.         }
  558.     }
  559.     else
  560.     {
  561.         if (theControl == theDoc->vScrollBar)
  562.             part = TrackControl(theControl, localPt, (ControlActionUPP) gVScrollActionUPP);
  563.         else
  564.             part = TrackControl(theControl, localPt, (ControlActionUPP) gHScrollActionUPP);
  565.     }
  566.  
  567.     SetPort(savePort);
  568. }
  569.  
  570.  
  571.  
  572. void DoBackgroundContent ( WindowPtr theWindow, EventRecord theEvent)
  573. {
  574.     Point            thePoint;
  575.     GrafPtr            savePort;
  576.     RgnHandle        dragRgn;
  577.     DPtr              theDoc;
  578.  
  579.     GetPort ( &savePort );
  580.     SetPort ( theWindow );
  581.     theDoc = DPtrFromWindowPtr ( theWindow );
  582.     thePoint = theEvent.where;
  583.     GlobalToLocal ( &thePoint );
  584.     
  585.     
  586.     dragRgn = NewRgn ( );
  587.     GetSelectedTextRgn ( theDoc, dragRgn );
  588.     if ( PtInRgn ( thePoint, dragRgn ) )
  589.     {
  590.         if ( DoWindowContentDrag ( theWindow, &theEvent ) )
  591.             SelectWindow (theWindow );
  592.     }
  593.     else
  594.         SelectWindow ( theWindow );
  595.     
  596.     SetPort ( savePort );
  597.  
  598.     return;
  599. }
  600.  
  601.  
  602.  
  603. /*
  604. pascal void DoContent(WindowPtr    theWindow,
  605.                       EventRecord  theEvent)
  606.  
  607.   {
  608.       short         cntlCode;
  609.       short         part;
  610.       ControlHandle theControl;
  611.       GrafPtr       savePort;
  612.       Boolean       extend;
  613.       DPtr          theDoc;
  614.       short         value;
  615.  
  616.     GetPort(&savePort);
  617.     SetPort(theWindow);
  618.     theDoc = DPtrFromWindowPtr(theWindow);
  619.  
  620.     GlobalToLocal(&theEvent.where);
  621.     cntlCode = FindControl(theEvent.where, theWindow, &theControl);
  622.     if (cntlCode == 0)
  623.       {
  624.               //only extend the selection if the shiftkey is down
  625.         extend = (theEvent.modifiers & shiftKey);
  626.  
  627.                 if (PtInRect(theEvent.where, &(*(theDoc->theText))->viewRect))
  628.                     TEClick(theEvent.where, extend, theDoc->theText);
  629.             }
  630.     else
  631.       if (cntlCode == kControlIndicatorPart)
  632.         {
  633.                     value = GetCtlValue(theControl);
  634.                     part  = TrackControl(theControl, theEvent.where, nil);
  635.                     if (part)
  636.                         {
  637.                             value -= GetCtlValue(theControl);
  638.                             if (value)
  639.                                 {
  640.                                     if (theControl == theDoc->vScrollBar)
  641.                                         TEScroll(0, value, theDoc->theText);
  642.                                     else
  643.                                         TEScroll(value, 0, theDoc->theText);
  644.                                     DrawPageExtras(theDoc);
  645.                                 }
  646.                         } 
  647.                 }
  648.             else
  649.                 if (theControl == theDoc->vScrollBar)
  650.                     part = TrackControl(theControl, theEvent.where, gVScrollActionUPP);
  651.                 else
  652.                     part = TrackControl(theControl, theEvent.where, gHScrollActionUPP);
  653.  
  654.         SetPort(savePort);
  655.     }
  656. */    
  657.  
  658. #pragma segment Window
  659.  
  660.  
  661. pascal OSErr DoActivate(WindowPtr theWindow, Boolean   activate)
  662. {
  663.     OSErr err;
  664.     Rect  r;
  665.     DPtr  theDoc;
  666.     
  667.  
  668.       err = noErr;
  669.             
  670.             if (theWindow)
  671.                 if (Ours(theWindow))
  672.                     {
  673.                         theDoc = DPtrFromWindowPtr(theWindow);
  674.                         SetPort(theWindow);
  675.                         DrawGrowIcon(theWindow);
  676.                         GetWinContentRect(theWindow, &r);
  677.                         InvalRect(&r);
  678.                         if (activate)
  679.                             {
  680.                                 TEActivate(theDoc->theText);
  681.                                 HiliteControl ( theDoc->vScrollBar, 0 );
  682.                                 HiliteControl ( theDoc->hScrollBar, 0 );
  683.                                 
  684.                                 DisableItem(myMenus[editM], undoCommand);
  685.                                 err = TEFromScrap();
  686.                                 if (gWCount == 0)
  687.                                     SetShortMenus();
  688.                             }
  689.                         else
  690.                             {
  691.                                 TEDeactivate(theDoc->theText);
  692.                                 HiliteControl ( theDoc->vScrollBar, 255 );
  693.                                 HiliteControl ( theDoc->hScrollBar, 255 );
  694.                                 
  695.                                 err = ZeroScrap();
  696.                                 err = TEToScrap();
  697.                             }
  698.                     }
  699.           return(err);
  700.         }
  701.  
  702. #pragma segment Window
  703.  
  704. pascal void GetPageEnds(short          pageHeight,
  705.                                               TEHandle       theText,
  706.                                               PageEndsArray  pageBounds,
  707.                                               short          *nPages)
  708.     {
  709.       short  pageBase;      /* total pixel offset of pages so far */
  710.       short  thisLine;
  711.       short  lastLine;
  712.       short  pageSoFar;
  713.       short  thisPage;      /* Current page being calced */
  714.       short  thisLineH;     /* Height of text line */
  715.       short  pageFirstLine; /* Line # of top of page */
  716.             
  717.         pageBase   = 0;
  718.         thisLine   = 1;
  719.         lastLine   = (*theText)->nLines;
  720.         
  721.         thisPage   = 0;
  722.         pageSoFar  = 0;
  723.         while ((thisLine <= lastLine) || (pageSoFar!=0))
  724.             {
  725.                 pageFirstLine = thisLine;
  726.                 thisLineH     = TEGetHeight(thisLine, thisLine, theText);
  727.                 
  728.                 while ((thisLineH+pageSoFar<pageHeight) && (thisLine <= lastLine))
  729.                     {
  730.                         pageSoFar += thisLineH;
  731.                         thisLine++;
  732.                         thisLineH = TEGetHeight(thisLine, thisLine, theText);
  733.                     }
  734.                     
  735.                 if (pageSoFar)
  736.                     {
  737.                         pageBounds[thisPage] = pageSoFar+pageBase;
  738.                         pageBase  = pageBounds[thisPage];
  739.                         thisPage++;
  740.                         pageSoFar = 0;
  741.                     }
  742.                     
  743.                 /*
  744.                     Special case text line taller than page
  745.                 */
  746.                 
  747.                 if ((thisLine  == pageFirstLine) &&
  748.                      (thisLineH > pageHeight))
  749.                     {
  750.                         do {
  751.                             pageBounds[thisPage] = pageBase+pageHeight;
  752.                             pageBase   = pageBounds[thisPage];
  753.                             thisPage  += 1;
  754.                             thisLineH -= pageHeight;
  755.                         } while (thisLineH >= pageHeight);
  756.                         pageSoFar = thisLineH; /* Carry bottom of large line to next page */
  757.                         thisLine += 1; /* carry xs on as pageSoFar and start measuring next line */
  758.                     }
  759.             }
  760.             
  761.         *nPages = thisPage;
  762.         
  763.     }  /* GetPageEnds */
  764.         
  765. pascal void DrawPageBreaks(DPtr theDoc)
  766.     {
  767.         PageEndsArray    pageEnds;
  768.         short           nPages;
  769.         short           ctr;
  770.         short           lineBase;
  771.         short           pageHeight;
  772.         Rect            viewRect;
  773.         Rect            pageRect;
  774.                     
  775.         GetRectOfPage( theDoc, &pageRect );
  776.         pageHeight = pageRect.bottom - pageRect.top;
  777.                 
  778.         GetPageEnds(pageHeight,
  779.                                 theDoc->theText,
  780.                                 pageEnds,
  781.                                 &nPages);
  782.                                             
  783.         lineBase = (*(theDoc->theText))->destRect.top;
  784.         viewRect = (*(theDoc->theText))->viewRect;
  785.  
  786.         PenPat(&qd.gray);
  787.         for (ctr = 0; ctr<nPages-1; ctr++)
  788.             {
  789.                 MoveTo(viewRect.left, lineBase+pageEnds[ctr]);
  790.                 LineTo(viewRect.right,lineBase+pageEnds[ctr]);
  791.             }
  792.         PenNormal();
  793.     } /*    DrawPageBreaks */
  794.             
  795. pascal void DrawPageExtras(DPtr theDoc)
  796. {
  797.     GrafPtr   oldPort;
  798.     RgnHandle    oldClip;
  799.     Rect          rectToClip;
  800.             
  801.     GetPort(&oldPort);
  802.     SetPort(theDoc->theWindow);
  803.                 
  804.     oldClip = NewRgn();
  805.     GetClip(oldClip);
  806.                 
  807.     GetWinContentRect(theDoc->theWindow,&rectToClip);
  808.     ClipRect(&rectToClip);
  809.                         
  810.     /* and then the page breaks */
  811.     DrawPageBreaks(theDoc);
  812.     
  813.     SetClip(oldClip);
  814.     
  815.     DisposeRgn(oldClip);
  816.     
  817.     SetPort(oldPort);
  818. }  /* DrawPageExtras */
  819.  
  820. pascal void DoUpdate ( WindowPtr theWindow )
  821. {
  822.     GrafPtr        savePort;
  823.     Rect        rectClip;
  824.     DPtr        theDocument;
  825.     
  826.     
  827.     theDocument = DPtrFromWindowPtr ( theWindow );
  828.     if ( Ours ( theWindow ) )
  829.     {
  830.         GetPort ( &savePort );
  831.         SetPort ( theWindow );
  832.         BeginUpdate ( theWindow );
  833.         
  834.         ClipRect(&theWindow->portRect);
  835.         EraseRect(&theWindow->portRect);
  836.         DrawControls(theWindow);
  837.         DrawGrowIcon(theWindow);
  838.         
  839.         GetWinContentRect(theWindow, &rectClip);
  840.         ClipRect(&rectClip);
  841.         
  842.         TEUpdate(&theWindow->portRect, theDocument->theText);
  843.         
  844.         DrawPageExtras(theDocument);
  845.         
  846.         EndUpdate(theWindow);
  847.         ClipRect(&theWindow->portRect);
  848.         
  849.         SetPort ( savePort );
  850.     }
  851.     
  852.     return;
  853. } /* DoUpdate */
  854.  
  855.  
  856.  
  857. #pragma segment Window
  858.  
  859. DPtr    NewDocument(Boolean isForOldDoc, WindowPtr behindWindow)
  860. {
  861.     Rect           destRect;
  862.     Rect           viewRect;
  863.     Rect           vScrollRect;
  864.     Rect           hScrollRect;
  865.     DPtr           myDoc;
  866.     WindowPtr      myWindow;
  867.     ControlHandle  vScroll;
  868.     ControlHandle  hScroll;
  869.     Str255         theName;
  870.     Str255         newNumber;
  871.     Rect           pageRect;
  872.  
  873.       if (!gWCount)
  874.           SetLongMenus();
  875.  
  876.         myDoc = nil;
  877.           myWindow = GetNewWindow(WindowID, nil, behindWindow);
  878.         if (myWindow)
  879.             {
  880.                 if (isForOldDoc==false)
  881.                     {
  882.                         GetWTitle(myWindow, theName);
  883.                         NumToString(++gNewDocCount, newNumber);
  884.                         if (gNewDocCount>1)
  885.                             {
  886.                                 PStrCat(theName, (unsigned char *)"\p #");
  887.                                 PStrCat(theName, newNumber);
  888.                                 SetWTitle(myWindow, theName);
  889.                             }
  890.                     }
  891.                     
  892.                 OffsetWindow(myWindow);
  893.  
  894.                 SetPort(myWindow);
  895.  
  896.                 myDoc = (DPtr)NewPtr(sizeof(DocRec));
  897.  
  898.                 SetWRefCon(myWindow, (long)myDoc);
  899.  
  900.                 myDoc->theWindow = myWindow;
  901.  
  902.                 vScrollRect = myWindow->portRect;
  903.  
  904.                 vScrollRect.left  = vScrollRect.right - kScrollbarAdjust;
  905.                 vScrollRect.right = vScrollRect.left  + kScrollbarWidth;
  906.  
  907.                 vScrollRect.bottom = vScrollRect.bottom - 14;
  908.                 vScrollRect.top    = vScrollRect.top - 1;
  909.                 
  910.                 vScroll = NewControl(myWindow, &vScrollRect, (unsigned char *)"\pScrollBar", true, 0, 0, 0, scrollBarProc, 0);
  911.  
  912.                 hScrollRect = myWindow->portRect;
  913.                 hScrollRect.top = hScrollRect.bottom - kScrollbarAdjust;
  914.                 hScrollRect.bottom = hScrollRect.top + kScrollbarWidth;
  915.  
  916.                 hScrollRect.right = hScrollRect.right - 14;
  917.                 hScrollRect.left  = hScrollRect.left - 1;
  918.                 hScroll = NewControl(myWindow, &hScrollRect, (unsigned char *)"\pScrollBar", true, 0, 0, 0, scrollBarProc, 0);
  919.  
  920.                 myDoc->vScrollBar = vScroll;
  921.                 myDoc->hScrollBar = hScroll;
  922.                 myDoc->lastID = 0;
  923.                 
  924.             
  925.                 myDoc->dirty = false;
  926.  
  927.                 GetTERect(myWindow, &viewRect);
  928.                 destRect = viewRect;
  929.  
  930.                 myDoc->theFont  = kFontIDTimes;
  931.                 myDoc->theStyle = 0;
  932.                 myDoc->theSize  = 12;
  933.  
  934.                 myDoc->documentJob   = nil;
  935.                 myDoc->thePrintSetup = nil;
  936.                 
  937.                 
  938.                 myDoc->thePrintSetup = (THPrint)NewHandle(sizeof(TPrint));
  939.                     
  940.                 PrOpen();
  941.                 PrintDefault(myDoc->thePrintSetup);
  942.                 PrClose();
  943.                 
  944.             
  945.             
  946.                 GetRectOfPage( myDoc, &pageRect );
  947.                 destRect.right = destRect.left + pageRect.right;
  948.                 
  949.                 OffsetRect(&destRect, kTextOffset, kTextOffset);
  950.                 
  951.                 TextFont(kFontIDTimes);
  952.                 TextSize(12);
  953.                 TextFace(0);
  954.  
  955.                 myDoc->theText = TEStyleNew(&destRect, &viewRect);
  956.  
  957.     /*
  958.     SetClikLoop(@AutoScroll, myDoc->theText);
  959.     */
  960.  
  961.                 myDoc->theFileName[0] = 0;
  962.                 myDoc->everSaved      = false;
  963.                 myDoc->theWindow      = myWindow;
  964.                 DoResizeWindow(myDoc);
  965.                 
  966.                 InstallDragHandlers ( myWindow );
  967.             }
  968.     return(myDoc);
  969.   }
  970.  
  971. #pragma segment Window
  972.  
  973. pascal void CloseMyWindow(WindowPtr aWindow)
  974. {
  975.     DPtr     aDocument;
  976.     TEHandle theText;
  977.  
  978.     HideWindow(aWindow);
  979.     aDocument = DPtrFromWindowPtr(aWindow);
  980.     
  981.     theText = aDocument->theText;
  982.     TEDispose(theText);
  983.     
  984.     if (aDocument->thePrintSetup)
  985.         DisposeHandle((Handle)aDocument->thePrintSetup);
  986.     
  987.     if (aDocument->documentJob)
  988.         //GXDisposeJob(aDocument->documentJob);
  989.     
  990.     RemoveDragHandlers ( aWindow );
  991.     
  992.     DisposePtr((Ptr)aDocument);
  993.     DisposeWindow(aWindow);
  994.     
  995.     gWCount--;
  996.     
  997.     /*if there are no more windows open, set up the short menus*/
  998.     if (gWCount == 0)
  999.         SetShortMenus();
  1000.     
  1001. }
  1002.  
  1003.             
  1004.     /*
  1005.         Name     : PrintWindow
  1006.         Function : Prints the document supplied in theDoc. askUser controls interaction
  1007.                    with the user.
  1008.                              
  1009.                              Uses extra memory equal to the size of the textedit use in the 
  1010.                              printed document.
  1011.     */
  1012.     
  1013. pascal void PrintWindow(DPtr theDoc, Boolean askUser)
  1014.     {
  1015.       GrafPtr         oldPort;
  1016.     TEHandle        printerTE;
  1017.     TPPrPort             printerPort;
  1018.         Rect                     printView;
  1019.          PageEndsArray     pageBounds;
  1020.          short                  nPages;
  1021.          short                   pageCtr;
  1022.          Boolean                 abort;
  1023.          Rect                     rectToClip;
  1024.         TPrStatus           thePrinterStatus;
  1025.          DialogPtr             progressDialog;
  1026.          WindowPtr       tempWind;                 // Temp window to create TERec in 
  1027.         Rect            tempWindRect = {0,0,0,0}; // Bounds for temp window
  1028.         abort = false;
  1029.             
  1030.         /*
  1031.             Preserve the current port
  1032.         */
  1033.         GetPort(&oldPort);
  1034.         PrOpen();
  1035.             
  1036.             
  1037.       if (askUser)
  1038.             abort = !PrJobDialog(theDoc->thePrintSetup);
  1039.                 
  1040.         if (abort)
  1041.             {
  1042.                     PrClose();
  1043.                     return;
  1044.             }
  1045.             
  1046.         progressDialog = GetNewDialog(1005, nil, (WindowPtr)-1);
  1047.             
  1048.         DrawDialog(progressDialog);
  1049.             
  1050.         printerPort = PrOpenDoc(theDoc->thePrintSetup, nil, nil);
  1051.         SetPort((GrafPtr)printerPort);
  1052.             
  1053. // Create a temporary window(which is not shown)
  1054.  
  1055.         tempWind = NewWindow( nil, &tempWindRect, "\p",
  1056.                                                     false, documentProc, (WindowPtr)-1,
  1057.                                                     false, 0);
  1058.  
  1059. // Duplicate the text edit rec
  1060.         printView = (*(theDoc->thePrintSetup))->prInfo.rPage;
  1061.         DuplicateStyleTERec ( theDoc->theText,
  1062.                                                     &printerTE,
  1063.                                                     &printView,
  1064.                                                     (GrafPtr)tempWind );
  1065.  
  1066.         // Work out the offsets
  1067.         
  1068.         (*printerTE)->destRect = printView; // GetPageEnds calls TECalText
  1069.             
  1070.         GetPageEnds(printView.bottom-printView.top,
  1071.                                 printerTE,
  1072.                                 pageBounds,
  1073.                                 &nPages);
  1074.             
  1075.         TEDeactivate(printerTE);
  1076.  
  1077. // Set the TERec to the printer port
  1078.         (*printerTE)->inPort = (GrafPtr)printerPort;
  1079.  
  1080.         for (pageCtr = 0; pageCtr <= nPages-1; pageCtr++)
  1081.             if (!abort)
  1082.                 {
  1083.                     PrOpenPage(printerPort, nil);
  1084.                                                 
  1085.                     rectToClip = printView;
  1086.                     
  1087.                     if (pageCtr > 0)
  1088.                         rectToClip.bottom = rectToClip.top + (pageBounds[pageCtr]-pageBounds[pageCtr-1]);
  1089.                     else
  1090.                         rectToClip.bottom = rectToClip.top + pageBounds[pageCtr];
  1091.                         
  1092.                     ClipRect(&rectToClip);
  1093.                     
  1094.                     if (PrError() == iPrAbort)
  1095.                         abort = true;
  1096.                         
  1097.                     if (! abort)
  1098.                         TEUpdate(&printView, printerTE);
  1099.                                             
  1100.                     if (PrError() == iPrAbort)
  1101.                         abort = true;
  1102.                         
  1103.                     PrClosePage(printerPort);
  1104.                     
  1105.                     TEScroll(0,rectToClip.top-rectToClip.bottom, printerTE);
  1106.                 }
  1107.  
  1108.         TEDispose(printerTE);
  1109.         DisposeWindow ( tempWind );
  1110.         PrCloseDoc(printerPort);
  1111.  
  1112.         if (( (*(theDoc->thePrintSetup))->prJob.bJDocLoop == bSpoolLoop ) && 
  1113.                 ( PrError() == noErr )  &&
  1114.                 (! abort))
  1115.             PrPicFile( theDoc->thePrintSetup, nil, nil, nil, &thePrinterStatus);
  1116.         
  1117.         PrClose();
  1118.         
  1119.         DisposeDialog(progressDialog);
  1120.         
  1121.         SetPort(oldPort);
  1122.         InvalRect(&oldPort->portRect);
  1123.     }